#ifndef W25Q128_H
#define W25Q128_H

#include "spi.h"
#include "main.h"
#include "string.h"
#include "cmsis_os.h"

#define W25Q128_CS_H HAL_GPIO_WritePin(F_CS_GPIO_Port,F_CS_Pin,GPIO_PIN_SET)
#define W25Q128_CS_L HAL_GPIO_WritePin(F_CS_GPIO_Port,F_CS_Pin,GPIO_PIN_RESET)
#define HSPI hspi2



#define W25Q128_1     						0XFFFF
#define W25Q128_2									0x7FFF
#define W25Q128_3									0x0000

#define FLASH_Init							0xFF
#define FLASH_WriteEnable                   0x06 
#define FLASH_ReadStatusReg1                0x05 
#define FLASH_ReadData                      0x03 
#define FLASH_PageProgram                   0x02 
#define FLASH_SectorErase                   0x20 
#define FLASH_ChipErase                     0xC7 
#define FLASH_ManufactDeviceID            	0x90

typedef struct{
	uint16_t 	flash_type;
	
	uint8_t 	flash_init_flag;
	
	uint8_t 	flash_busy_flag;
	
	uint8_t 	flash_erase_all_flag;
	
	uint8_t 	flash_erase_addr_flag;
	uint32_t 	flash_erase_addr_set;
	
	uint8_t flash_write_buf[256];
	uint8_t flash_write_flag;
	uint32_t flash_write_address;
	uint16_t flash_write_len;
	
	uint8_t flash_read_buf[256];
	uint32_t flash_read_address;
	uint8_t flash_read_flag;
	uint16_t flash_read_len;
	
}w25q128_data_t;

extern w25q128_data_t w25q128_data;

void spi_send(uint8_t data_send);
uint8_t spi_send_read(uint8_t cmd);

void w25q128_config_init(void);
void w25q128_init(void);
void w25q128_send_address(uint32_t address);
void w25q128_read_busy_if(void);
void w25q128_write_enable(void);
void w25q128_erase_chip(void);
void w25q128_erase_sector(uint32_t addr);
uint8_t w25q128_read(uint8_t *puf,uint32_t addr,uint16_t datalen);
void w25q128_write_page(uint8_t *puf,uint32_t addr,uint16_t datalen);
void w25q128_write_page_nocheck(uint8_t *puf,uint32_t addr,uint16_t datalen);
void w25q128_write(uint8_t *puf,uint32_t addr,uint16_t datalen);
void w25q128_wait_busy(void);
void buf_clear(uint8_t *buf,uint16_t len);
uint16_t w25q128_read_id(void);



#endif


